package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TalTalentInfo;
import com.ruoyi.system.enums.ChangeStateEnum;
import com.ruoyi.system.service.ITalTalentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TalChangeApplyMapper;
import com.ruoyi.system.domain.TalChangeApply;
import com.ruoyi.system.service.ITalChangeApplyService;
import org.springframework.util.CollectionUtils;

/**
 * 人才变动申请Service业务层处理
 *
 * @author zhusq
 * @date 2023-04-30
 */
@Service
public class TalChangeApplyServiceImpl extends ServiceImpl<TalChangeApplyMapper,TalChangeApply> implements ITalChangeApplyService
{
    @Autowired
    private TalChangeApplyMapper talChangeApplyMapper;

    @Autowired
    private ITalTalentInfoService talentInfoService ;

    /**
     * 查询人才变动申请
     *
     * @param id 人才变动申请主键
     * @return 人才变动申请
     */
    @Override
    public TalChangeApply selectTalChangeApplyById(Long id)
    {
        return talChangeApplyMapper.selectTalChangeApplyById(id);
    }

    /**
     * 查询人才变动申请列表
     *
     * @param talChangeApply 人才变动申请
     * @return 人才变动申请
     */
    @Override
    public List<TalChangeApply> selectTalChangeApplyList(TalChangeApply talChangeApply)
    {
        List<TalChangeApply> list = talChangeApplyMapper.selectTalChangeApplyList(talChangeApply);
        for (TalChangeApply changeApply : list) {
            changeApply.setStateDescribe(ChangeStateEnum.getMessageByCode(changeApply.getState()));
        }
        return list ;
    }

    /**
     * 新增人才变动申请
     *
     * @param talChangeApply 人才变动申请
     * @return 结果
     */
    @Override
    public int insertTalChangeApply(TalChangeApply talChangeApply) {
        /**数据权限*/
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        for (SysRole role : roles) {
            if ("common".equals(role.getRoleKey()) || "talent".equals(role.getRoleKey())) {
                throw new ServiceException("你不是管理人员！") ;
            }
        }
//        LambdaQueryWrapper<TalTalentInfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(TalTalentInfo::getUserId,SecurityUtils.getUserId()) ;
//        List<TalTalentInfo> list = talentInfoService.list(wrapper);
//        if (CollectionUtils.isEmpty(list)){
//            throw new ServiceException("你不是管理人员！") ;
//        }
        talChangeApply.setCreateTime(DateUtils.getNowDate());
        talChangeApply.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        talChangeApply.setName(SecurityUtils.getLoginUser().getUser().getNickName());
        talChangeApply.setUserId(SecurityUtils.getUserId());
        return talChangeApplyMapper.insertTalChangeApply(talChangeApply);
    }

    /**
     * 修改人才变动申请
     *
     * @param talChangeApply 人才变动申请
     * @return 结果
     */
    @Override
    public int updateTalChangeApply(TalChangeApply talChangeApply)
    {
        talChangeApply.setUpdateTime(DateUtils.getNowDate());
        return talChangeApplyMapper.updateTalChangeApply(talChangeApply);
    }

    /**
     * 批量删除人才变动申请
     *
     * @param ids 需要删除的人才变动申请主键
     * @return 结果
     */
    @Override
    public int deleteTalChangeApplyByIds(Long[] ids)
    {
        return talChangeApplyMapper.deleteTalChangeApplyByIds(ids);
    }

    /**
     * 删除人才变动申请信息
     *
     * @param id 人才变动申请主键
     * @return 结果
     */
    @Override
    public int deleteTalChangeApplyById(Long id)
    {
        return talChangeApplyMapper.deleteTalChangeApplyById(id);
    }

    @Override
    public void applyChange(Long changeId, Integer state) {
        TalChangeApply apply = new TalChangeApply();
        apply.setId(changeId);
        apply.setState(state);
        apply.setUpdateTime(new Date());
        boolean update = this.updateById(apply);
        if (!update){
            throw new ServiceException("审批失败！") ;
        }
    }
}
